#include "Mod_NetConfig.h"

/**
 *  函数功能: 
 * 
 * 
 */ 

NetCfg_TypeDef NetworkConfig_Read()
{    
    NetCfg_TypeDef config;  // 用于存储读取的内容
    W25Q64_ReadData(NET_CONFIG_ADDRESS, (uint8_t *)&config, sizeof(NetCfg_TypeDef));
    return config;     
}

SocketConfig SocketCfg_Read()
{
    SocketConfig config;  // 用于存储读取的内容
    W25Q64_ReadData(Socket_CONFIG_ADDRESS, (uint8_t *)&config, sizeof(SocketConfig));
    return config;     
}

bool UartConfig_ReadAll(UART_ConfigTypeDef configs_out[])
{
    UART_AllConfigs readCfg;
    W25Q64_ReadData(UART_CONFIG_ADDRESS, (uint8_t *)&readCfg, sizeof(readCfg));

    if (readCfg.magic != UART_CONFIG_MAGIC)
    {
        // 默认值
        for (int i = 0; i < UART_PORT_NUM; i++) {
            configs_out[i] = g_defaultUartConfig;
        }
        return false;  // 表示使用的是默认值
    }

    memcpy(configs_out, readCfg.configs, sizeof(UART_ConfigTypeDef) * UART_PORT_NUM);
    return true;
}

// 读取4个结构体的数据，返回指向结构体数组的指针
//RemotNetCfg_TypeDef* RemoteNetCfg_Read()
//{
//    static RemotNetCfg_TypeDef configs[4];  // 静态数组，确保在函数返回后数据不会丢失
//    W25Q64_ReadData(RemoteNet_CONFIG_ADDRESS, (uint8_t *)configs, sizeof(RemotNetCfg_TypeDef) * 4);
//    return configs;
//}

void NetworkConfig_Write(NetCfg_TypeDef config)
{
    W25Q64_SectorErase(NET_CONFIG_ADDRESS);	
    W25Q64_PageProgram(NET_CONFIG_ADDRESS, (uint8_t*)&config, sizeof(NetworkConfig));
}

void SocketConfig_Write(SocketConfig config)
{
    W25Q64_SectorErase(Socket_CONFIG_ADDRESS);	
    W25Q64_PageProgram(Socket_CONFIG_ADDRESS, (uint8_t*)&config, sizeof(SocketConfig));
}

void UartConfig_WriteAll(UART_ConfigTypeDef configs[])
{
    UART_AllConfigs allCfg;
    allCfg.magic = UART_CONFIG_MAGIC;
    memcpy(allCfg.configs, configs, sizeof(UART_ConfigTypeDef) * UART_PORT_NUM);
    W25Q64_SectorErase(UART_CONFIG_ADDRESS);
    W25Q64_PageProgram(UART_CONFIG_ADDRESS, (uint8_t *)&allCfg, sizeof(allCfg));
}


void printNetConfig(NetCfg_TypeDef *cfg)
{
    printf("IP Address     : %d.%d.%d.%d\n", cfg->auch_SIP[0], cfg->auch_SIP[1], cfg->auch_SIP[2], cfg->auch_SIP[3]);
    printf("Gateway        : %d.%d.%d.%d\n", cfg->auch_GA[0], cfg->auch_GA[1], cfg->auch_GA[2], cfg->auch_GA[3]);
    printf("Subnet Mask    : %d.%d.%d.%d\n", cfg->auch_SUB[0], cfg->auch_SUB[1], cfg->auch_SUB[2], cfg->auch_SUB[3]);
    printf("MAC Address    : %02X:%02X:%02X:%02X:%02X:%02X\n", cfg->auch_SHA[0], cfg->auch_SHA[1], cfg->auch_SHA[2], cfg->auch_SHA[3], cfg->auch_SHA[4], cfg->auch_SHA[5]);
    printf("Retry Timeout  : %d ms\n", cfg->uin_RTR);
    printf("Retry Count    : %d\n", cfg->uch_RCR);
}


NetCfg_TypeDef localnet_config;   // 这里定义了具体的变量，用来做全局变量
SocketConfig socket_config;  
//UART_ConfigTypeDef uart_config;  
UART_ConfigTypeDef g_uartConfigs[UART_PORT_NUM];	

//写入4个结构体数据 
//void RemoteNetworkConfig_Write(RemotNetCfg_TypeDef *config)
//{
//    // 擦除存储区域
//    W25Q64_SectorErase(RemoteNet_CONFIG_ADDRESS);

//    // 直接写入4组数据
//    W25Q64_PageProgram(RemoteNet_CONFIG_ADDRESS, (uint8_t *)config, sizeof(RemotNetCfg_TypeDef) * 4);

//    //printf("Successfully wrote 4 configurations to flash.\n");
//}